package de.java.shortys.mbca.app.view;

import static de.java.shortys.core.utils.view.ViewLocationUtilityConstants.LOCATION_CENTER;
import static de.java.shortys.core.utils.view.ViewUtils.setLocation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Locale;

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import de.java.shortys.mbca.api.lang.LanguageObservable;
import de.java.shortys.mbca.api.view.AbstractApplicationView;
import de.java.shortys.mbca.api.view.IView;
import de.shortys.myhome.api.view.components.sidebar.AbstractSidebar;
import de.shortys.myhome.api.view.components.sidebar.ApplicationSidebar;
import de.shortys.myhome.api.view.components.toolbar.AbstractToolbar;
import de.shortys.myhome.api.view.components.toolbar.ApplicationToolbar;

public class ApplicationView extends AbstractApplicationView {

	private JPanel component;

	private JPanel contentPane;

	private AbstractToolbar toolbar;

	private AbstractSidebar sidebar;

	private JPanel body;

	public ApplicationView(String title) {
		super(title);
	}

	@Override
	public void activate(IView view) {
		body.add(view.build());
		body.revalidate();
		body.repaint();
	}

	@Override
	public void deactivate(IView view) {
		body.remove(view.build());
		body.revalidate();
		body.repaint();
	}

	@Override
	protected void buildComponents() {
		buildComponent();
		buildContentPane();
		buildToolbar();
		buildSidebar();
		buildBody();
	}

	private void buildBody() {
		body = new JPanel();
		body.setLayout(new BorderLayout());
		body.setOpaque(false);
		contentPane.add(body, "dock center");
	}

	private void buildSidebar() {
		sidebar = new ApplicationSidebar();
		contentPane.add(sidebar.build(), "dock west");
	}

	private void buildToolbar() {
		toolbar = new ApplicationToolbar();
		toolbar.getContentPane().setOpaque(false);
		toolbar.getContentPane().setPreferredSize(new Dimension(0, 50));
		contentPane.add(toolbar.build(), "dock north");

	}

	private void buildComponent() {
		configureWindow();
		component = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				Graphics2D graphics2d = (Graphics2D) g;
				super.paintComponent(g);

				graphics2d.setPaint(new GradientPaint(0, 0, Color.decode("0x01448b"), component.getSize().width,
						component.getSize().height, Color.decode("0x0093c7")));
				graphics2d.fillRect(0, 0, component.getSize().width, component.getSize().height);
			};
		};
		component.setLayout(new BorderLayout());
		add(component);
	}

	private void configureWindow() {
		getWindow().setSize(1000, 700);
		setLocation(LOCATION_CENTER, getWindow());
	}

	private void buildContentPane() {
		contentPane = new JPanel();
		contentPane.setOpaque(false);
		contentPane.setLayout(new MigLayout("", "0[grow]", "0[grow]"));
		component.add(contentPane);
	}

	public JPanel getContentPane() {
		return contentPane;
	}

	@Override
	public void update(LanguageObservable observable, Locale newLanguage) {
		// Empty
	}

	@Override
	public AbstractToolbar getToolbar() {
		return toolbar;
	}

	@Override
	public AbstractSidebar getSidebar() {
		return sidebar;
	}

}
